<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <%layout("/WEB-INF/view/common/inc/admin_head.html",{title:'', styles:'bootstrap,font-awesome,animate,style,bootstrap-table,layui-date,auto-complete,dtvalidate,layui'}){%>
    <style type="text/css">
        body{padding-top:10px;}
        *{margin:0;padding:0;list-style-type:none;}
        a,img{border:0;}
        .demo h2{font-size:16px;color:#3366cc;height:30px;}
        .demo li{float:left;}
    </style>
    <%}%>
</head>

<body>
<form>
    <div id="toolbar" class="btn-group">
        <button  class="add-btn layui-btn layui-btn-normal layui-btn-sm" style="margin-top: -6px;margin-left: -4px">
            <i class="fa fa-plus" aria-hidden="true"></i> 新增品类
        </button>
        <button  class="export-btn layui-btn  layui-btn-sm" style="margin-top: -6px;margin-left: -4px">
            <i class="fa fa-download" aria-hidden="true"></i> 导出当前数据
        </button>
    </div>
</form>
<div class="wrapper wrapper-content">
    <!-- START 数据表格 -->
    <table id="portTable"
           data-toolbar="#toolbar"
           data-side-pagination="server"
           data-pagination="true"
           data-resizable="true"
           data-url="/sea/product/category/findCategory.do"
           data-page-size="15"
           class="table table-striped table-hover"
    >
    </table>
    <!-- END 数据表格 -->
</div>

<div id="layer_window" style="display: none;overflow-x: hidden;overflow-y: hidden">
    <!-- START 内容部分 -->
    <div class="container" >
        <form class="form-horizontal" id="info-form" method="post" autocomplete="off" style="margin: auto">
            <input type="hidden" id="categoryParentId" name="categoryParentId">
            <div class="form-group" style="margin-top: 20px">
                <label class="category control-label col-sm-2" style="max-width: 100px">商品大类</label>
                <div class="col-sm-6" style="max-width: 300px">
                    <input type="text" id="categoryName" name="categoryName" value="" class="form-control">
                </div>
            </div>
            <div class="form-group" style="margin-top: 20px">
                <label class="control-label col-sm-7" style="max-width: 100px"></label>
                <button type="button" class="save-btn btn btn-primary">
                    <i class="fa fa-save"></i> 保存
                </button>
            </div>
        </form>
    </div>
    <!-- END 内容部分 -->
</div>
<%layout("/WEB-INF/view/common/inc/admin_js.html",{modules:'jquery,bootstrap,bootstrap-table,dtvalidate,dtvalidate-lang-zh_cn,bootstrap-table-zh-CN,laydate,layer'}){}%>
<script>
    var $portTable = $('#portTable');
    $(function(){

        //START 表格配置
        $portTable.bootstrapTable({
            pageSize:10,
            detailView: true,
            columns: [
                {
                    title: '大类',
                    field: 'categoryName',
                    align: 'center',
                    valign: 'middle'
                },{
                    title: '税率',
                    field: '',
                    align: 'center',
                    valign: 'middle',
                    formatter:function (v,r) {
                        return '13%';
                    }
                },{
                    field: 'operate',
                    title: '操作',
                    align: 'center',
                    valign: 'middle',
                    formatter: optFormatter
                }
            ],
            onLoadSuccess : function(){
                $('.add-mid-btn').on('click',function () {
                    var id = $(this).attr('data-val');
                    $("#categoryParentId").val(id);
                    $(".category").html("商品中类");
                    $("#categoryName").val("");
                    layer.open({
                        type:1,
                        title:'添加品类',
                        area:['600px','250px'],
                        skin:'layui-layer-lan',//layui-layer-molv ;layui-layer-lan;layui-layer-rim;layer-window
                        shadeClose:false,
                        resize: false,
                        scrollbar: false,
                        maxmin: false,//出现最大化按钮
                        content:$('#layer_window')
                    })
                });

                $('.edit-big-btn').on('click',function(){//新增
                    var id=$(this).attr("data-val");
                    layer.open({
                        type: 2,
                        area: ['600px','400px'],
                        skin: 'layui-layer-molv',
                        content: ['/sea/product/category/editBigPage.do?id='+id, 'yes']
                    });
                });
            },
            onExpandRow: function (index, row, $detail) {
                InitSubTable(index, row, $detail);
            }
        });
        //END 表格配置
    });

    var cur_table;
    //初始化子表格(无线循环)
    function InitSubTable (index, row, $detail) {
        var parentId = row.id;
        cur_table = $detail.html('<table id="child_table" class="table table-striped table-hover"></table>').find('table');
        $(cur_table).bootstrapTable({
            url: '/sea/product/category/findByParentId',
            method: 'get',
            clickToSelect: true,
            queryParams: { id: parentId },
            ajaxOptions: { id: parentId },
            //height: 500,
            uniqueId: "id",
            pageSize: 10,
            pageList: [10, 25],
            detailView: true,
            columns: [
                {
                    field: 'categoryName',
                    title: '中类',
                    align: 'center'
                },{
                    field: 'operate',
                    title: '操作',
                    align: 'center',
                    valign: 'middle',
                    formatter: optFormatter2
                }],
            onLoadSuccess : function(){
                $('.add-class-btn').on('click',function () {
                    var id = $(this).attr('data-val');
                    $("#categoryParentId").val(id);
                    $(".category").html("商品小类");
                    $("#categoryName").val("");
                    layer.open({
                        type:1,
                        title:'添加品类',
                        area:['600px','250px'],
                        skin:'layui-layer-lan',//layui-layer-molv ;layui-layer-lan;layui-layer-rim;layer-window
                        shadeClose:false,
                        resize: false,
                        scrollbar: false,
                        maxmin: false,//出现最大化按钮
                        content:$('#layer_window')
                    })
                });

                $('.edit-mid-btn').on('click',function(){//新增
                    var id=$(this).attr("data-val");
                    layer.open({
                        type: 2,
                        area: ['600px','400px'],
                        skin: 'layui-layer-molv',
                        content: ['/sea/product/category/editMidPage.do?id='+id, 'yes']
                    });
                });
            },
            onExpandRow: function (index, row, $detail) {
                InitSubTable2(index, row, $detail);
            }
        });
    }
    function InitSubTable2 (index, row, $detail) {
        var parentId = row.id;
        cur_table = $detail.html('<table id="child_table2" class="table table-striped table-hover"></table>').find('table');
        $(cur_table).bootstrapTable({
            url: '/sea/product/category/findByParentId',
            method: 'get',
            clickToSelect: true,
            queryParams: { id: parentId },
            ajaxOptions: { id: parentId },
            //height: 500,
            uniqueId: "id",
            pageSize: 10,
            pageList: [10, 25],
            detailView: false,
            columns: [
                {
                    field: 'categoryName',
                    title: '小类',
                    align: 'center'
                },{
                    field: 'operate',
                    title: '操作',
                    align: 'center',
                    valign: 'middle',
                    formatter: optFormatter3
                }],
            onLoadSuccess : function(){
                $('.edit-btn').on('click',function(){//新增
                    var id=$(this).attr("data-val");
                    layer.open({
                        type: 2,
                        area: ['600px','400px'],
                        skin: 'layui-layer-molv',
                        content: ['/sea/product/category/editPage.do?id='+id, 'yes']
                    });
                });
            }
        });
    }

    function optFormatter2(value,row){
        var content = '<button class="add-class-btn btn btn-xs btn-success" data-val="'+row.id+'" title="添加小类"><i class="fa fa-plus"></i>  </button>&nbsp;&nbsp;';
        content += '<button class="edit-mid-btn btn btn-xs  btn-danger" data-val="'+row.id+'" title="修改中类"><i class="fa fa-edit"></i>  修改中类</button>';
        return content;
    }

    function optFormatter3(value,row){
        var content = '<button class="edit-btn btn btn-xs btn-info" data-val="'+row.id+'" title="修改小类"><i class="fa fa-edit"></i>  修改小类</button>';
        return content;
    }


    // 操作菜单
    function optFormatter(value,row){
        var content = '<button class="add-mid-btn btn btn-xs btn-info" data-val="'+row.id+'" title="添加中类"><i class="fa fa-plus"></i>  </button>&nbsp;&nbsp;';
        content += '<button class="edit-big-btn btn btn-xs  btn-success" data-val="'+row.id+'" title="修改大类"><i class="fa fa-edit"></i>  修改大类</button>';
        return content;
    }

    $('.add-btn').on('click',function(){
        $(".category").html("商品大类");
        $("#categoryName").val("");
        $("#categoryParentId").val("");
        layer.open({
            type:1,
            title:'添加品类',
            area:['600px','250px'],
            skin:'layui-layer-lan',//layui-layer-molv ;layui-layer-lan;layui-layer-rim;layer-window
            shadeClose:false,
            resize: false,
            scrollbar: false,
            maxmin: false,//出现最大化按钮
            content:$('#layer_window')
        })
    });


    //提交表单
    $('.save-btn').on('click',function(){
        layer.confirm('确定要新增当前品类？', {
            btn: ['确定','取消'],title:'提示'
        }, function(){
            var categoryName=$("#categoryName").val();
            if(categoryName==''){
                parent.layer.msg('请填写品类名称！',{icon: 0,time:1500,shade: [0.8, '#393D49']});
                return;
            }
            layer.closeAll();
            var index=layer.load();
            $.post('/sea/product/category/save.do',{categoryName:categoryName,categoryParentId:$("#categoryParentId").val()},function(data){
                if( data.ok ){
                    var categoryParentId=$("#categoryParentId").val();
                    if(categoryParentId==null || categoryParentId==''){
                        $portTable.bootstrapTable('refresh');
                    }else{
                        cur_table.bootstrapTable('refresh');
                    }
                    layer.msg(data.msg,{icon:1,time:1500,shade: [0.8, '#393D49']});
                    layer.close(index);
                    $("#categoryParentId").val("");
                }else{
                    layer.msg(data.msg,{icon: 0,time:1500,shade: [0.8, '#393D49']});
                    layer.close(index);
                    $("#categoryParentId").val("");
                }
            });
        });
    });
    $(".export-btn").click(function () {
        window.location.href='/sea/product/category/export.do?'+$('#search-form').serialize();
    });
</script>
</body>
</html>
